package com.yanxibao.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yanxibao.bean.Blog;
import com.yanxibao.common.Result;
import com.yanxibao.dto.BlogDTO;
import com.yanxibao.dto.PageQueryDTO;
import com.yanxibao.service.BlogService;
import com.yanxibao.mapper.BlogMapper;
import com.yanxibao.util.RedisConstants;
import com.yanxibao.util.UserHolder;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.yanxibao.util.WechatPatterns.TEST_ID;

/**
 * 博客服务实现
 *
 * @author kongke
 * 2023-11-19 20:12:25
 */
@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog>
    implements BlogService{

    @Resource
    private BlogMapper blogMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Result<Blog> addBlog(Blog blog) {
//      设置用户id
//        String userId = UserHolder.getUser().getUserId();
        //        测试
        blog.setUserId(TEST_ID);

        boolean save = save(blog);

//      TODO 消息推送

        return save ? Result.success(blog) : Result.error("500","添加失败");
    }

    @Override
    public Result<Boolean> deleteBlog(Integer id) {
        boolean remove = removeById(id);
        return remove ? Result.success(true) : Result.error("500","删除失败");
    }

    @Override
    public Result<Blog> updateBlog(Blog blog) {
        boolean update = updateById(blog);
        return update ? Result.success(blog) : Result.error("500","更新失败");
    }

    @Override
    public Result<Blog> queryById(Integer blogId) {
        Blog blog = getById(blogId);

        return blog != null ? Result.success(blog) : Result.error("500","查询失败");
    }

    @Override
    public Result<PageQueryDTO<BlogDTO>> queryBlog(String userId) {
        PageQueryDTO<BlogDTO> dto = new PageQueryDTO<>();

//        如果userId是空则返回所有博客
        List<BlogDTO> list = blogMapper.queryBlog(userId);

        dto.setList(list);
        dto.setCount((long) list.size());

        return Result.success(dto);
    }

    @Override
    public Result<Integer> likeBlog(Integer blogId) {

//        String userId = UserHolder.getUser().getUserId();
//        测试
        String userId = TEST_ID;

        String key = RedisConstants.LIKES_BLOG_KEY + blogId;
//      查询是否已经点赞
        Boolean isLike = stringRedisTemplate.opsForSet().isMember(key, userId);

        if (Boolean.TRUE.equals(isLike)){
//            点过则取消点赞
            boolean update = update().setSql("likes = likes - 1").eq("id", blogId).update();

            if (update) {
                stringRedisTemplate.opsForSet().remove(key,userId);
                return Result.success(0);
            }


        }else {
            boolean update = update().setSql("likes = likes + 1").eq("id", blogId).update();

            if (update) {
                stringRedisTemplate.opsForSet().add(key,userId);
                return Result.success(1);
            }

        }

        return Result.error("500","操作失败");
    }

    @Override
    public Result<Boolean> isLike(Integer blogId) {
//        String userId = UserHolder.getUser().getUserId();
//        测试
        String userId = TEST_ID;

        String key = RedisConstants.LIKES_BLOG_KEY + blogId;
//      查询是否已经点赞
        Boolean isLike = stringRedisTemplate.opsForSet().isMember(key, userId);
        return Result.success(isLike);
    }
}




